<?php


namespace Lrental\Utils;


use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;

class OrderNumberGenerator
{
    const EXPIRETIME = 24 * 3600; // 1天
    const EXPIRETIME_DEBUG = 60; // 1分钟

    public static function id()
    {
        $now = Carbon::now();
        // under debug model the key will expired in 1 minute else it will be 1 day
        $key = config('app.debug') ? $now->format('YmdHi') : $now->format('Ymd');
        $sequenceNo = Redis::incr($key);
        if ($sequenceNo == 1) {
            $expire = config('app.debug') ? self::EXPIRETIME_DEBUG : self::EXPIRETIME;
            Redis::expire($key, $expire);
        }
        $_seq = sprintf('%03d', $sequenceNo);
        $id = $now->format('YmdHis') . $_seq;
        return $id;
    }
}
